import requests
from django.db import models
from django.contrib.auth.models import User


# Create your models here.

class Topic(models.Model):
    """用户学习的主题"""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now=True)

    #建立到模型User的外键关系。用户被删除时，所有与之相关的主题也会被删除。保证topic归属指定用户
    owner = models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        """返回模型的字符串表示"""
        return self.text

class Entry(models.Model):
    """学到的有关某个主题的具体知识"""
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name_plural = 'entries'

    def __str__(self):
        """返回模型的字符串表示"""
        # return self.text[:50]

        if len(self.text) > 50:
            return f"{self.text[:10]}..."
        else:
            return f"{self.text}"
